/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bean;

import com.common.json.ParseJSONObject;
import com.entity.Employee;
import com.model.EmployeeModel;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import org.json.simple.JSONObject;

/**
 *
 * @author _HungChilvas_
 */
@Named(value = "listEmployee")
@SessionScoped
public class ListEmployee implements Serializable {

    ParseJSONObject parserJSonOb = new ParseJSONObject();
    EmployeeModel employeeModel = new EmployeeModel();
    private String mail;

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }
    Employee employee;

    private List<Employee> lstEmployee;

    public List<Employee> getLstEmployee() {
        return lstEmployee;
    }

    public void setLstEmployee(List<Employee> lstEmployee) {
        this.lstEmployee = lstEmployee;
    }

    /**
     * Creates a new instance of ListEmployee
     */
    public ListEmployee() {
        //lstEmployee = listEmployee();
    }

    public List<Employee> listEmployee() {
        String jsonlstEmployee = null;
        if (name != null) {
            jsonlstEmployee=employeeModel.listSearchEmployee(name);
        } else {
            jsonlstEmployee = employeeModel.listEmployee();
        }
        lstEmployee = new ArrayList<>();
        for (int i = 0; i < parserJSonOb.parseJSON(jsonlstEmployee).size(); i++) {
            JSONObject jsonObject = (JSONObject) parserJSonOb.parseJSON(jsonlstEmployee).get(i);
            employee = new Employee();
            employee.setUsername((String) jsonObject.get("username"));
            employee.setAddress((String) jsonObject.get("address"));
            employee.setFullname((String) jsonObject.get("fullname"));
            employee.setAvatar((String) jsonObject.get("avatar"));
            employee.setAvatar((String) jsonObject.get("email"));
            lstEmployee.add(employee);
        }

        return lstEmployee;
    }
    private static final long serialVersionUID = 1L;

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSayWelcome() {
        //check if null?
        if ("".equals(name) || name == null) {
            return "";
        } else {
            return "Ajax message : Welcome " + name;
        }
    }
}
